package com.website.system.controller;

import com.website.common.controller.BaseController;
import com.website.core.aop.HandlingTime;
import com.website.system.dao.UserMapper;
import com.website.system.model.User;
import com.website.system.utils.GetNumber;
import com.website.system.utils.SendEmail;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller
public class RegisterController extends BaseController {

    @Resource
    private UserMapper userMapper;

    /**
     * 跳转到注册页面
     */
    @RequestMapping(value = ("/register"), method = RequestMethod.GET)
    public String register() {
        return "/manage/register";
    }

    /**
     * 用户注册
     */
    @ResponseBody
    @RequestMapping(value="/registerUser",method = RequestMethod.POST)
    public String registerUser(@ModelAttribute User user,String birthdayToDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            user.setBirthday(sdf.parse(birthdayToDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        userMapper.insert(user);
        return SUCCESS_MSG;
    }

    /**
     * 获取手机验证码
     */
    @ResponseBody
    @RequestMapping(value="/getCodeMessage",method = RequestMethod.POST)
    public String getCodeMessage(){
        String codeName= GetNumber.getRandNumber();
        return codeName;
    }

    /**
     * 注册成功向用户邮箱发送信息
     */
    @ResponseBody
    @RequestMapping(value = "/sendEmail",method = RequestMethod.GET)
    public void sendEmail(String userEmail){
        String userName = "2278928586@qq.com"; // 发件人QQ邮箱
        String password = "enbrgnfgvwdxdjdh";//激活POP3/SMTP服务的授权码
        String smtpHost = "smtp.qq.com"; // 邮件服务器
        String fromName = "庞庞科技";//发件人名称
        String to = userEmail; // 收件人的邮箱账号，多个收件人以半角逗号分隔
        String subject = "测试信息"; // 主题
        String body = "【庞庞科技】恭喜你注册成功，可以使用了，厉害厉害。"; // 正文，可以用html格式的哟
        SendEmail email = SendEmail.entity(smtpHost, userName, password, to, subject, body, fromName);
        try {
            email.send(); // 发送！
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
